/*
 * Copyright 2007 www.luzand.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


var yrprxWebNavigationHelper = {};

yrprxWebNavigationHelper.openNewTabInCurrentBrowser = function ()
{
    var newTab = gBrowser.addTab('about:blank');
    var newBrowser = gBrowser.getBrowserForTab(newTab);

    gBrowser.tabContainer.selectedItem = newTab;

    return newBrowser.webNavigation;
}

yrprxWebNavigationHelper.openNewTab = function ()
{
    var mediator = yrprxComponentsFactory.getWindowMediatorService();

    var navigatorBrowser = mediator.getMostRecentWindow("navigator:browser");
    if (!navigatorBrowser)
        return this.createNewWindow();

    var tabBrowser = navigatorBrowser.getBrowser();

    var newTab = tabBrowser.addTab('about:blank');
    var newBrowser = tabBrowser.getBrowserForTab(newTab);

    tabBrowser.tabContainer.selectedItem = newTab;

    return newBrowser.webNavigation.getInterface(Components.interfaces.nsIWebNavigation);
}

yrprxWebNavigationHelper.createNewWindow = function ()
{
    var browser = null;

    var windowWatcher = yrprxComponentsFactory.getWindowWatcherService();

    var magicWnd = windowWatcher.openWindow(null, 'about:blank', null, null, null);

    var wnd = magicWnd.open();

    browser = wnd.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIWebNavigation);

    magicWnd.close();

    return browser;
}

yrprxWebNavigationHelper.openNewWindow = function ()
{
    var mediator = yrprxComponentsFactory.getWindowMediatorService();

    var navigatorBrowser = mediator.getMostRecentWindow("navigator:browser");
    if (!navigatorBrowser)
        return this.createNewWindow();

    var wnd = navigatorBrowser.open();

    return wnd.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIWebNavigation);
}

yrprxWebNavigationHelper.postData = function (webNavigation, url, multipartFormDataRequest)
{
    var headers = "Content-type: " + multipartFormDataRequest.contentType + "\r\n" +
                  "Content-length: " + multipartFormDataRequest.getContentLength() + "\r\n";

    var stream = multipartFormDataRequest.toInputStream();

    var multiplex = stream.QueryInterface(Components.interfaces.nsIMultiplexInputStream);
    // Strange bug... add "\r\n" before body to fix it
    multiplex.insertStream(yrprxComponentsFactory.createStringInputStream("\r\n"), 0);

    webNavigation.loadURI(url, Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE, null, stream, yrprxComponentsFactory.createStringInputStream(headers));
}

yrprxWebNavigationHelper.openInNewTab = function (url)
{
    this.openNewTab().loadURI(url, Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
}

yrprxWebNavigationHelper.openInNewWindow = function (url)
{
    this.openNewWindow().loadURI(url, Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
}
